using System;
using System.IO;
using System.Net;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xaml;

namespace RESTAgent.Xaml {

    public class XamlFormatter : MediaTypeFormatter {

        public XamlFormatter() {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xaml+xml"));
        }

        protected override Task<object> OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext)
        {
            return new TaskFactory<object>().StartNew(() => XamlServices.Load(stream));
        }

        protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
        {
            return new TaskFactory().StartNew(() => XamlServices.Save(stream, value));
        }
     
    }
}
